#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

namespace F { // max_flow_with_cost
    const int N = 6010;
    const int M = N * N;
    const int inf = 0x3f3f3f3f;
    int h[N],now[N],cnt = 1,S,T;
    bool vis[N];
    ll maxflow,cost,dis[N];
    struct edge { int v,w,c,nxt; } e[M << 1];
    inline void tlink(int x,int y,int w,int c) { e[++cnt] = (edge) { y,w,c,h[x] }; h[x] = cnt; }
    inline void link(int x,int y,int w,int c) { tlink(x,y,w,c); tlink(y,x,0,-c); }
    inline void setst(int s,int t) { S = s; T = t; }
    bool bfs() {
	queue < int > q; q.push(S); memset(dis,0x3f,sizeof dis); memset(vis,0,sizeof vis); dis[S] = 0; now[S] = h[S]; vis[S] = 1;
	while(!q.empty()) {
	    int x = q.front(); q.pop();
	    for(int i = h[x],y;i;i = e[i].nxt)
		if(e[i].w && dis[y = e[i].v] > dis[x] + e[i].c) {
		    dis[y] = dis[x] + e[i].c; now[y] = h[y];
		    if(!vis[y]) vis[y] = 1,q.push(y);
		}
	    vis[x] = 0;
	} return dis[T] < inf;
    }
    int dinic(int x,int flow,ll c) {
	if(x == T) return maxflow += flow,cost += c * flow,flow;
	int res = flow; vis[x] = 1;
	for(int i = now[x],y;i && res;i = e[i].nxt) {
	    now[x] = i;
	    if(e[i].w && dis[y = e[i].v] == dis[x] + e[i].c && !vis[y]) {
		int k = dinic(y,min(res,e[i].w),c + e[i].c); 
		e[i].w -= k; e[i ^ 1].w += k; res -= k;
	    }
	} vis[x] = 0;
	return flow - res;
    }
    void runit() { while(bfs()) dinic(S,inf,0); }
};

const int N = 5010;
const int M = 1010;

int n,m,a[N],b[N],c[N],d[N],s[M],t[M],deg[N];
ll ans;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in,m = in;
    for(int i = 1;i <= n;i++) a[i] = in,b[i] = in,c[i] = in,d[i] = in;
    for(int i = 1;i <= m;i++) s[i] = in,t[i] = in,deg[s[i]]++,deg[t[i]]++;
    for(int i = 1;i <= m;i++) F :: link(0,n + i,1,0),F :: link(n + i,s[i],1,0),F :: link(n + i,t[i],1,0);
    for(int i = 1;i <= n;i++) b[i] += deg[i],ans += (ll) c[i] * a[i] * a[i] + (ll) d[i] * b[i] * b[i];
    for(int i = 1;i <= n;i++)
	while(deg[i])
	    F :: link(i,n + m + 1,1,2ll * c[i] * a[i] + c[i] + d[i] - 2ll * d[i] * b[i]),deg[i]--,a[i]++,b[i]--;
    F :: setst(0,n + m + 1); F :: runit();
    ans += F :: cost; printf("%lld\n",ans);
    return 0;
}
